package com.hnkywl.sts.action.cwgl.gsjyys;

import com.hnkywl.sts.entity.cwgl.gsjyys.Gsjyys;
import com.hnkywl.sts.entity.organ.Gsjg;
import com.hnkywl.sts.entity.sys.Gncz;
import com.hnkywl.sts.service.cwgl.gsjyys.GsjyysService;
import com.hnkywl.sts.service.sys.GnczService;
import com.hnkywl.sts.service.sys.YgService;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.hibernate.tool.hbm2x.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: ccr
 * Date: 13-6-16
 * Time: 下午5:20
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/cwgl/gsjyys")
public class GsjyysAction extends SimpleActionSupport {
    private GsjyysService gsjyysService;
    private Integer nf;
    private Integer yf;
    private String x;
    private Long gsId;
    private Long bmId;
    private Long id;
    private int page;
    private int rows;
    private Gsjyys gsjyys;
    private List<Map> items;
    private String type;
    private Map<String,Map> hz;
    private List<Map> gsjgList;

    //功能操作
    private List<Gncz> gnczList;
    private GnczService gnczService;

    @Action("gsjyys")
    public String gsjyys(){
        gnczList = gnczService.findGnczByJsAndUrl(request.getServletPath());
        nf = Calendar.getInstance().get(Calendar.YEAR);
        yf = Calendar.getInstance().get(Calendar.MONTH)+1;
        gsId = YgService.getCurrentYgZgGsId();
        return SUCCESS;
    }

    @Action("gsjyysList")
    public void gsjyysList(){
        renderJson(gsjyysService.gsjyysGrid(gsId,bmId,nf,yf,x,page,rows));
    }
    @Action("savemx")
    public void savemx(){
        try{
            gsjyysService.save(gsjyys);
            renderText("{\"statusCode\":200}");
        } catch (Exception e){
            e.printStackTrace();
            renderText("{\"statusCode\":300,\"message\":\"" + e.getMessage() + "\"}");
        }
    }
    @Action("gsjyys_content")
    public String gsjyysContent(){
        if(id != null){
            gsjyys = gsjyysService.findDetail(id);
            gsId = gsjyys.getGs().getId();
            if(gsjyys.getBm() != null){
                bmId = gsjyys.getBm().getId();
            }
            nf = gsjyys.getNf();
            yf = gsjyys.getYf();
            x = gsjyys.getX();
        } else {
            items = gsjyysService.findMx(type);
        }
        return SUCCESS;
    }

    @Action("gsjyys_hz")
    public String gsjyysHz(){
        nf = Integer.valueOf(new SimpleDateFormat("yyyy").format(new Date()));
        yf = Integer.valueOf(new SimpleDateFormat("MM").format(new Date()));
        int d = Integer.valueOf(new SimpleDateFormat("dd").format(new Date()));
        x = d < 20 ? (d<10 ? "上旬" : "中旬") : "下旬";
        gsjgList = gsjyysService.findGsJc();
        return SUCCESS;
    }
    @Action("hzList")
    public void findHzList(){
        if(StringUtils.isEmpty(x)){
            int d = Integer.valueOf(new SimpleDateFormat("dd").format(new Date()));
            x = d < 20 ? (d<10 ? "上旬" : "中旬") : "下旬";
        }
        renderJson(gsjyysService.findHz(nf,yf,x));
    }
    @Action("delete")
    public void delete(){
        try{
            gsjyysService.delete(id);
            renderJson("{\"statusCode\":200}");
        } catch (Exception e){
            e.printStackTrace();
            renderJson("{\"statusCode\":200,\"message\":\""+e.getMessage()+"\"}");
        }
    }

    @Action("submit")
    public void submit(){
        try{
            gsjyysService.submit(id);
            renderJson("{\"statusCode\":200}");
        } catch (Exception e){
            e.printStackTrace();
            renderJson("{\"statusCode\":200,\"message\":\""+e.getMessage()+"\"}");
        }
    }

    @Action("sh")
    public void sh(){
        try{
            gsjyysService.sh(id);
            renderJson("{\"statusCode\":200}");
        } catch (Exception e){
            e.printStackTrace();
            renderJson("{\"statusCode\":200,\"message\":\""+e.getMessage()+"\"}");
        }
    }

    //=======getter setter========
    public Integer getNf() {
        return nf;
    }

    public void setNf(Integer nf) {
        this.nf = nf;
    }

    public Integer getYf() {
        return yf;
    }

    public void setYf(Integer yf) {
        this.yf = yf;
    }

    public String getX() {
        return x;
    }

    public void setX(String x) {
        this.x = x;
    }

    public Long getGsId() {
        return gsId;
    }

    public void setGsId(Long gsId) {
        this.gsId = gsId;
    }

    public Long getBmId() {
        return bmId;
    }

    public void setBmId(Long bmId) {
        this.bmId = bmId;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public Gsjyys getGsjyys() {
        return gsjyys;
    }

    public void setGsjyys(Gsjyys gsjyys) {
        this.gsjyys = gsjyys;
    }

    public List<Map> getItems() {
        return items;
    }

    public void setItems(List<Map> items) {
        this.items = items;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public List<Map> getGsjgList() {
        return gsjgList;
    }

    public List<Gncz> getGnczList() {
        return gnczList;
    }

    public void setGnczList(List<Gncz> gnczList) {
        this.gnczList = gnczList;
    }

    @Autowired
    public void setGnczService(GnczService gnczService) {
        this.gnczService = gnczService;
    }

    //=======setter======
    @Autowired
    public void setGsjyysService(GsjyysService gsjyysService) {
        this.gsjyysService = gsjyysService;
    }
}
